package org.dftproject.genesis.ui.editors;

import org.dftproject.genesis.ui.Activator;
import org.dftproject.genesis.ui.pages.IPageInput;
import org.dftproject.genesis.ui.pages.IPagePart;
import org.dftproject.genesis.ui.pages.PageHistoryEntry;
import org.dftproject.genesis.ui.pages.PageInitException;
import org.dftproject.genesis.ui.pages.PageManager;
import org.dftproject.genesis.ui.pages.PageSite;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorSite;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.contexts.IContextService;
import org.eclipse.ui.part.EditorPart;

public class TabPart extends EditorPart {

	public static final String ID = TabPart.class.getName();

	private IPagePart pagePart;

	private Composite container;

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.ui.part.EditorPart#init(org.eclipse.ui.IEditorSite,
	 *      org.eclipse.ui.IEditorInput)
	 */
	@Override
	public void init(IEditorSite site, IEditorInput input) throws PartInitException {
		if (!(input instanceof TabPartEditorInput))
			throw new PartInitException("input must be a " + TabPartEditorInput.class.getName());
		setSite(site);
		setInput(input);

		initPage();
	}

	public TabPartEditorInput getTabPartEditorInput() {
		return (TabPartEditorInput) getEditorInput();
	}

	@Override
	public void createPartControl(Composite parent) {
		IContextService service = (IContextService) getSite().getService(IContextService.class);
		service.activateContext(Activator.PAGE_CONTEXT);
		
		createPage(parent);
	}

	public void initPage() {
		pagePart = PageManager.createPagePart(getTabPartEditorInput().getPageID());
		if (pagePart != null) {
			try {
				IPageInput pageInput = getTabPartEditorInput().getPageInput();
				
				pagePart.init(new PageSite(getEditorSite()), pageInput);
				
				this.setPartName(pageInput.getLabel());
				this.setTitleImage(pageInput.getImageDescriptor().createImage());
			} catch (PageInitException e) {
				e.printStackTrace(System.err);
			}
		}
	}

	public void navigate() {
		Composite parent = container.getParent();
		disposePage();
		initPage();
		createPage(parent);
	}
	
	public void navigateNoNotify() {
		Composite parent = container.getParent();
		disposePage();
		initPage();
		createPageNoNotify(parent);
	}

	public void createPage(Composite parent) {
		createPageNoNotify(parent);

		PageHistoryEntry entry = new PageHistoryEntry();
		entry.setPageID(getTabPartEditorInput().getPageID());
		entry.setPageInput(getTabPartEditorInput().getPageInput());
		getTabPartEditorInput().getPageHistory().addEntry(entry);
	}
	
	public void createPageNoNotify(Composite parent) {
		container = new Composite(parent, SWT.NONE);
		container.setBackground(parent.getDisplay().getSystemColor(SWT.COLOR_LIST_BACKGROUND));
		container.setBackgroundMode(SWT.INHERIT_DEFAULT);
		container.setLayout(new FillLayout());
		pagePart.createPartControl(container);
		parent.layout(true);
		setFocus();
	}

	public void disposePage() {
		if (container != null) {
			container.dispose();
			container = null;
		}
	}

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.ui.part.WorkbenchPart#setFocus()
	 */
	@Override
	public void setFocus() {
		pagePart.setFocus();
	}

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.ui.part.EditorPart#doSave(org.eclipse.core.runtime.IProgressMonitor)
	 */
	@Override
	public void doSave(IProgressMonitor monitor) {
	}

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.ui.part.EditorPart#doSaveAs()
	 */
	@Override
	public void doSaveAs() {
	}

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.ui.part.EditorPart#isDirty()
	 */
	@Override
	public boolean isDirty() {
		return false;
	}

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.ui.part.EditorPart#isSaveAsAllowed()
	 */
	@Override
	public boolean isSaveAsAllowed() {
		return false;
	}

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.ui.part.WorkbenchPart#dispose()
	 */
	@Override
	public void dispose() {
		super.dispose();
	}

}
